package com.syq.service.impl;

import com.syq.constant.CacheConstants;
import com.syq.constant.RabbitMQConstants;
import com.syq.constant.SourceType;
import com.syq.feign.CacheFeign;
import com.syq.model.StandardSubmit;
import com.syq.service.FilterChainManager;
import com.syq.service.FilterChainService;
import com.syq.util.PushMsgUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName: FLiterChainManagerImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/27 9:41
 */

@Service
public class FLiterChainManagerImpl implements FilterChainManager {

    @Autowired
    private Map<String, FilterChainService> filterChain;

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void executeFilter(StandardSubmit standardSubmit) {
        Integer clientID = standardSubmit.getClientID();
        String key= CacheConstants.CACHE_PREFIX_CLIENT+clientID;
        String filters = cacheFeign.hget(key, "filters");
        String[] splitArray = filters.split(",");
        for (String filter : splitArray) {
            FilterChainService filterChainService = filterChain.get(filter);
            if(filterChainService!=null) {
                boolean strategy = filterChainService.strategy(standardSubmit);
                if (!strategy) {
                    //执行失败，打印日志
                    pushMsgUtil.sendLog(standardSubmit, filter);
                    //执行失败，给客户发送信息
                    if(SourceType.HTTP == standardSubmit.getSource()){
                        pushMsgUtil.pushReport(standardSubmit, filter);
                    }
                    break;
                }
            }
        }
        rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_SMS_SEND_LOG + "_exchange","routingKey",standardSubmit);
    }
}
